#ifdef _WIN32 
#include <windows.h> 
#endif

#include "Builder.h"

Builder::Builder(glm::vec3 _Pos, float _r, float _speed, std::string _Name,
		std::string _modelFName, ModelPool *_mPool, PathFinder *_pathfinder,
		Terrain::Terrain *_ter) :
		Villager(_Pos, _r, _speed, _Name, _modelFName, _mPool, _pathfinder, _ter)
{

	type = OBJ_BUILDER;
	parentType = OBJ_VILLAGER;

	for (int i = 0; i < 33; i++)
	{
		std::stringstream ss; //create a stringstream
		ss << i + 1; //add number to the stream
		modelID.push_back(mPool->loadModel(modelFName, "_a_00000" + ss.str()));
	}

	costGold = 100;

	targetBuilding = NULL;
}

Builder::~Builder(void)
{
}

void Builder::setWork(Building *building)
{
	if (building != NULL)
	{
		targetBuilding = building;

		// set to new job status
		status = STATUS_NEWJOB;
	}
	else
		status = STATUS_READY;
}

void Builder::doWork()
{
	if (targetBuilding->getAge() < MAX_AGE)
	{
		// update in time interval building age
		if (buildingTimer.GetInterval(BUILD_INTERVAL))
		{
			targetBuilding->setAge(targetBuilding->getAge() + BUILD_STEP);
		}
	}
	else
	{
		// done
		status = STATUS_DONE;

		// save money for taxman to owner building
		if (owner != NULL)
		{
			Building *building = static_cast<Building*>(owner);
			building->incMoney(TAX);
		}
	}

}

void Builder::goWork()
{
	goTo(targetBuilding->get2dPosition());
	status = STATUS_GOWORK;
}

void Builder::goingWork()
{
	// check in time interval building status
	// when building is already done go home
	if (chckWhileMoveTimer.GetInterval(CHCK_WHILE_MOVE_INTERVAL)
			&& (targetBuilding->getStatus() == STATUS_READY))
		status = STATUS_DONE;
}

